<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <div class="login-banner" />
            <el-form ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">{{ title }}</h3>
                </div>
                <div>
                    <el-form-item prop="username">
                        <el-input ref="name" v-model="form.username" placeholder="用户名" type="text" tabindex="1" auto-complete="on">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="form.password" :type="passwordType" placeholder="密码" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="password" />
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                </div>
                <el-checkbox v-model="form.remember">记住我</el-checkbox>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
                <!--                <div style="margin-top: 20px; margin-bottom: -10px; color: #666; font-size: 14px; text-align: center; font-weight: bold;">-->
                <!--                    <span style="margin-right: 5px;">测试账号：</span>-->
                <!--                    <el-button type="danger" size="mini" plain @click="testAccount('test6','123456')">test6</el-button>-->

                <!--                    <el-button type="danger" size="mini" plain @click="testAccount('test7','1234567')">test7</el-button>-->
                <!--                </div>-->
            </el-form>
        </div>
        <!--        <Copyright v-if="$store.state.settings.showCopyright" />-->

        <!--        <el-dialog title="磁盘空间不足" :visible.sync="detailVisible"  width="800px">-->
        <!--            <h1>剩余空间还有：{{msg.disk}}G</h1>-->
        <!--            <el-button size="mini" type="primary" plain-->
        <!--                       @click="btnok">-->
        <!--                关闭-->
        <!--            </el-button>-->

        <!--        </el-dialog>-->
    </div>
</template>

<script>
// import { WarningDiskCapacity } from '@/api/login';
export default {
    name: 'Login',
    data() {
        return {
            // title: 888,
            title: process.env.VUE_APP_TITLE,
            form: {
                username: localStorage.login_account || '',
                password: localStorage.login_pwd || '',
                remember: !!localStorage.login_account
            },
            rules: {
                username: [
                    { required: true, trigger: 'blur', message: '请输入用户名' }
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' }
                ]
            },
            // msg: {
            //     disk: 1000
            // },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            detailVisible: false
        };
    },

    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true
        }
        // 'form.remember': function(val) {
        //     if (!val) {
        //         localStorage.removeItem('login_account');
        //         localStorage.removeItem('login_pwd');
        //     }
        // }
    },
    methods: {
        // btnok() {
        //     this.$refs.form.validate(valid => {
        //         if (valid) {
        //             this.loading = true;
        //             this.$store.dispatch('user/login', this.form).then(res => {
        //                 this.loading = false;
        //
        //                 if (res.LoginStatus == 'T') {
        //                     if (this.form.remember) {
        //                         localStorage.setItem('login_account', this.form.username);
        //                         localStorage.setItem('login_pwd', this.form.password);
        //                     } else {
        //                         localStorage.removeItem('login_account');
        //                         localStorage.removeItem('login_pwd');
        //                     }
        //                     this.$router.push({ path: '/dashboard' });
        //
        //                 } else {
        //                     this.$message.error('登录失败，用户名或密码错误');
        //                 }
        //
        //             }).catch(() => {
        //                 this.$message.error('用户名或密码错误');
        //                 this.loading = false;
        //             });
        //         }
        //     });
        // },
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password';
            this.$nextTick(() => {
                this.$refs.password.focus();
            });
        },
        handleLogin() {
            // debugger
            // WarningDiskCapacity().then(res => {
            //     if (res.res.WarningDiskCapacity < 500) {
            //         this.msg.disk = res.res.WarningDiskCapacity;
            //         this.detailVisible = true;
            //     } else {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true;
                    this.$store.dispatch('user/login', this.form).then(res => {
                        debugger
                        this.loading = false;

                        if (res.LoginStatus == 'T') {
                            if (this.form.remember) {
                                localStorage.setItem('login_account', this.form.username);

                                localStorage.setItem('login_pwd', this.form.password);
                            } else {
                                localStorage.removeItem('login_account');
                                localStorage.removeItem('login_pwd');
                            }
                            this.$router.push({ path: '/dashboard' });

                        } else {
                            this.$message.error('登录失败，用户名或密码错误');
                        }

                    }).catch(() => {
                        this.$message.error('用户名或密码错误');
                        this.loading = false;
                    });
                }
            }).catch(err => {
                console.log(err);
            });
            // }
            // this.msg.disk = res.res.WarningDiskCapacity;

            // if(this.msg.disk < 500){
            //     this.detailVisible = true
            // }
            // else {
            //     this.$refs.form.validate(valid => {
            //         if (valid) {
            //             this.loading = true;
            //             this.$store.dispatch('user/login', this.form).then(res => {
            //                 this.loading = false;
            //
            //                 if (res.LoginStatus == 'T') {
            //                     if (this.form.remember) {
            //                         localStorage.setItem('login_account', this.form.username);
            //                         localStorage.setItem('login_pwd', this.form.password);
            //                     } else {
            //                         localStorage.removeItem('login_account');
            //                         localStorage.removeItem('login_pwd');
            //                     }
            //                     this.$router.push({ path: '/dashboard' });
            //
            //                 } else {
            //                     this.$message.error('登录失败，用户名或密码错误');
            //                 }
            //
            //             }).catch(() => {
            //                 this.$message.error('用户名或密码错误');
            //                 this.loading = false;
            //             });
            //         }
            //     });
            // }

        },
        testAccount(account, pwd) {
            this.form.username = account;
            this.form.password = pwd;
            this.handleLogin();
        }
    }
};
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/login-bg.jpg);
    background-size: cover;
    background-position: center center;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/login-banner.jpg);
        background-size: cover;
        background-position: center center;
    }
    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
</style>
